home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / newmat08.zip / SUBMAT.CPP < prev    next >
C/C++ Source or Header  |  1995-01-15  |  8KB  |  278 lines

  1. //$$ submat.cpp                         submatrices
  2.  
  3. // Copyright (C) 1991,2,3,4: R B Davies
  4.  
  5. #include "include.h"
  6.  
  7. #include "newmat.h"
  8. #include "newmatrc.h"
  9.  
  10.  
  11. //#define REPORT { static ExeCounter ExeCount(__LINE__,11); ++ExeCount; }
  12.  
  13. #define REPORT {}
  14.  
  15.  
  16. /****************************** submatrices *********************************/
  17.  
  18. #ifdef TEMPS_DESTROYED_QUICKLY
  19. GetSubMatrix& BaseMatrix::SubMatrix(int first_row, int last_row, int first_col,
  20.    int last_col) const
  21. #else
  22. GetSubMatrix BaseMatrix::SubMatrix(int first_row, int last_row, int first_col,
  23.    int last_col) const
  24. #endif
  25. {
  26.    REPORT
  27.    Tracer tr("SubMatrix");
  28.    int a = first_row - 1; int b = last_row - first_row + 1;
  29.    int c = first_col - 1; int d = last_col - first_col + 1;
  30.    if (a<0 || b<=0 || c<0 || d<=0) Throw(SubMatrixDimensionException());
  31. #ifdef TEMPS_DESTROYED_QUICKLY
  32.    GetSubMatrix* x = new GetSubMatrix(this, a, b, c, d, FALSE);
  33.    MatrixErrorNoSpace(x);
  34.    return *x;
  35. #else
  36.    return GetSubMatrix(this, a, b, c, d, FALSE);
  37. #endif
  38. }
  39.  
  40. #ifdef TEMPS_DESTROYED_QUICKLY
  41. GetSubMatrix& BaseMatrix::SymSubMatrix(int first_row, int last_row) const
  42. #else
  43. GetSubMatrix BaseMatrix::SymSubMatrix(int first_row, int last_row) const
  44. #endif
  45. {
  46.    REPORT
  47.    Tracer tr("SubMatrix(symmetric)");
  48.    int a = first_row - 1; int b = last_row - first_row + 1;
  49.    if (a<0 || b<=0) Throw(SubMatrixDimensionException());
  50. #ifdef TEMPS_DESTROYED_QUICKLY
  51.    GetSubMatrix* x = new GetSubMatrix(this, a, b, a, b, TRUE);
  52.    MatrixErrorNoSpace(x);
  53.    return *x;
  54. #else
  55.    return GetSubMatrix( this, a, b, a, b, TRUE);
  56. #endif
  57. }
  58.  
  59. #ifdef TEMPS_DESTROYED_QUICKLY
  60. GetSubMatrix& BaseMatrix::Row(int first_row) const
  61. #else
  62. GetSubMatrix BaseMatrix::Row(int first_row) const
  63. #endif
  64. {
  65.    REPORT
  66.    Tracer tr("SubMatrix(row)");
  67.    int a = first_row - 1;
  68.    if (a<0) Throw(SubMatrixDimensionException());
  69. #ifdef TEMPS_DESTROYED_QUICKLY
  70.    GetSubMatrix* x = new GetSubMatrix(this, a, 1, 0, -1, FALSE);
  71.    MatrixErrorNoSpace(x);
  72.    return *x;
  73. #else
  74.    return GetSubMatrix(this, a, 1, 0, -1, FALSE);
  75. #endif
  76. }
  77.  
  78. #ifdef TEMPS_DESTROYED_QUICKLY
  79. GetSubMatrix& BaseMatrix::Rows(int first_row, int last_row) const
  80. #else
  81. GetSubMatrix BaseMatrix::Rows(int first_row, int last_row) const
  82. #endif
  83. {
  84.    REPORT
  85.    Tracer tr("SubMatrix(rows)");
  86.    int a = first_row - 1; int b = last_row - first_row + 1;
  87.    if (a<0 || b<=0) Throw(SubMatrixDimensionException());
  88. #ifdef TEMPS_DESTROYED_QUICKLY
  89.    GetSubMatrix* x = new GetSubMatrix(this, a, b, 0, -1, FALSE);
  90.    MatrixErrorNoSpace(x);
  91.    return *x;
  92. #else
  93.    return GetSubMatrix(this, a, b, 0, -1, FALSE);
  94. #endif
  95. }
  96.  
  97. #ifdef TEMPS_DESTROYED_QUICKLY
  98. GetSubMatrix& BaseMatrix::Column(int first_col) const
  99. #else
  100. GetSubMatrix BaseMatrix::Column(int first_col) const
  101. #endif
  102. {
  103.    REPORT
  104.    Tracer tr("SubMatrix(column)");
  105.    int c = first_col - 1;
  106.    if (c<0) Throw(SubMatrixDimensionException());
  107. #ifdef TEMPS_DESTROYED_QUICKLY
  108.    GetSubMatrix* x = new GetSubMatrix(this, 0, -1, c, 1, FALSE);
  109.    MatrixErrorNoSpace(x);
  110.    return *x;
  111. #else
  112.    return GetSubMatrix(this, 0, -1, c, 1, FALSE);
  113. #endif
  114. }
  115.  
  116. #ifdef TEMPS_DESTROYED_QUICKLY
  117. GetSubMatrix& BaseMatrix::Columns(int first_col, int last_col) const
  118. #else
  119. GetSubMatrix BaseMatrix::Columns(int first_col, int last_col) const
  120. #endif
  121. {
  122.    REPORT
  123.    Tracer tr("SubMatrix(columns)");
  124.    int c = first_col - 1; int d = last_col - first_col + 1;
  125.    if (c<0 || d<=0) Throw(SubMatrixDimensionException());
  126. #ifdef TEMPS_DESTROYED_QUICKLY
  127.    GetSubMatrix* x = new GetSubMatrix(this, 0, -1, c, d, FALSE);
  128.    MatrixErrorNoSpace(x);
  129.    return *x;
  130. #else
  131.    return GetSubMatrix(this, 0, -1, c, d, FALSE);
  132. #endif
  133. }
  134.  
  135. void GetSubMatrix::SetUpLHS()
  136. {
  137.    REPORT
  138.    Tracer tr("SubMatrix(LHS)");
  139.    const BaseMatrix* bm1 = bm;
  140.    GeneralMatrix* gm1 = ((BaseMatrix*&)bm)->Evaluate();
  141.    if ((BaseMatrix*)gm1!=bm1)
  142.       Throw(ProgramException("Invalid LHS"));
  143.    if (row_number < 0) row_number = gm1->Nrows();
  144.    if (col_number < 0) col_number = gm1->Ncols();
  145.    if (row_skip+row_number > gm1->Nrows()
  146.       || col_skip+col_number > gm1->Ncols())
  147.          Throw(SubMatrixDimensionException());
  148. }
  149.  
  150. void GetSubMatrix::operator<<(const BaseMatrix& bmx)
  151. {
  152.    REPORT
  153.    Tracer tr("SubMatrix(<<)"); GeneralMatrix* gmx = 0;
  154.    Try
  155.    {
  156.       SetUpLHS(); gmx = ((BaseMatrix&)bmx).Evaluate();
  157.       if (row_number != gmx->Nrows() || col_number != gmx->Ncols())
  158.          Throw(IncompatibleDimensionsException());
  159.       MatrixRow mrx(gmx, LoadOnEntry); 
  160.       MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  161.                                      // do need LoadOnEntry
  162.       MatrixRowCol sub; int i = row_number;
  163.       while (i--)
  164.       {
  165.          mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  166.          sub.Copy(mrx); mr.Next(); mrx.Next();
  167.       }
  168.       gmx->tDelete();
  169. #ifdef TEMPS_DESTROYED_QUICKLY
  170.       delete this;
  171. #endif
  172.    }   
  173.  
  174.    CatchAll
  175.    {
  176.       if (gmx) gmx->tDelete();
  177. #ifdef TEMPS_DESTROYED_QUICKLY
  178.       delete this;
  179. #endif
  180.       Bounce;
  181.    }
  182. }
  183.  
  184. void GetSubMatrix::operator=(const BaseMatrix& bmx)
  185. {
  186.    REPORT
  187.    Tracer tr("SubMatrix(=)"); GeneralMatrix* gmx = 0;
  188.    MatrixConversionCheck mcc;         // Check for loss of info
  189.    Try
  190.    {
  191.       SetUpLHS(); gmx = ((BaseMatrix&)bmx).Evaluate();
  192.       if (row_number != gmx->Nrows() || col_number != gmx->Ncols())
  193.          Throw(IncompatibleDimensionsException());
  194.       MatrixRow mrx(gmx, LoadOnEntry); 
  195.       MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  196.                                      // do need LoadOnEntry
  197.       MatrixRowCol sub; int i = row_number;
  198.       while (i--)
  199.       {
  200.          mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  201.          sub.CopyCheck(mrx); mr.Next(); mrx.Next();
  202.       }
  203.       gmx->tDelete();
  204. #ifdef TEMPS_DESTROYED_QUICKLY
  205.       delete this;
  206. #endif
  207.    }   
  208.  
  209.    CatchAll
  210.    {
  211.       if (gmx) gmx->tDelete();
  212. #ifdef TEMPS_DESTROYED_QUICKLY
  213.       delete this;
  214. #endif
  215.       Bounce;
  216.    }
  217. }
  218.  
  219. void GetSubMatrix::operator<<(const Real* r)
  220. {
  221.    REPORT
  222.    Tracer tr("SubMatrix(<<Real*)");
  223.    SetUpLHS();
  224.    if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
  225.       Throw(SubMatrixDimensionException());
  226.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  227.                                   // do need LoadOnEntry
  228.    MatrixRowCol sub; int i = row_number;
  229.    while (i--)
  230.    {
  231.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  232.       sub.Copy(r); mr.Next();
  233.    }
  234. #ifdef TEMPS_DESTROYED_QUICKLY
  235.    delete this;
  236. #endif
  237. }   
  238.  
  239. void GetSubMatrix::operator=(Real r)
  240. {
  241.    REPORT
  242.    Tracer tr("SubMatrix(=Real)");
  243.    SetUpLHS();
  244.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  245.                                   // do need LoadOnEntry
  246.    MatrixRowCol sub; int i = row_number;
  247.    while (i--)
  248.    {
  249.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  250.       sub.Copy(r); mr.Next();
  251.    }
  252. #ifdef TEMPS_DESTROYED_QUICKLY
  253.    delete this;
  254. #endif
  255. }   
  256.  
  257. void GetSubMatrix::Inject(const GeneralMatrix& gmx)
  258. {
  259.    REPORT
  260.    Tracer tr("SubMatrix(inject)");
  261.    SetUpLHS();
  262.    if (row_number != gmx.Nrows() || col_number != gmx.Ncols())
  263.       Throw(IncompatibleDimensionsException());
  264.    MatrixRow mrx((GeneralMatrix*)(&gmx), LoadOnEntry);
  265.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  266.                                   // do need LoadOnEntry
  267.    MatrixRowCol sub; int i = row_number;
  268.    while (i--)
  269.    {
  270.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  271.       sub.Inject(mrx); mr.Next(); mrx.Next();
  272.    }
  273. #ifdef TEMPS_DESTROYED_QUICKLY
  274.    delete this;
  275. #endif
  276. }
  277.  
  278.